package ee.ut.math.chess.base.model.chesspiece;

import java.util.ArrayList;
import java.util.List;

import ee.ut.math.chess.base.model.Move;
import ee.ut.math.chess.game.model.Game;

public class Knight extends ChessPiece{

	private static final long serialVersionUID = 1L;
	private static String[][] pattern = {
		{" ","X"," ","X"," "},
		{"X"," "," "," ","X"},
		{" "," ","O"," "," "},
		{"X"," "," "," ","X"},
		{" ","X"," ","X"," "}
	};
	private long value = 3;

	public Knight(Color color) {
		this.color = color;
	}
	

	@Override
	public void setInInitialPosition(boolean inInitialPosition) {
		//not important;
	}

	@Override
	public boolean isInInitialPosition() {
		return false;//not important
	}

	@Override
	public List<Move> getLegalMoves(int x, int y, Game game, boolean onlyKingThreatening) {
		List<Move> result = new ArrayList<Move>();
		for (int i=0; i < 5; i++) {
			for (int j=0;j < 5; j++ ) {
				if ("X".equals(pattern[i][j])) {
					int targetX = x - 2 + i;
					int targetY = y - 2+ j;
					if (targetX >=0 && targetX < 8 && targetY >=0 && targetY < 8 ) {
						ChessPiece chessPiece = game.getBoard().getChessPiece(targetX, targetY);
						if (chessPiece == null || chessPiece.getColor() != color) {
							result.add(new Move(x,y,targetX, targetY));
						}
					}
				}
			}
		}
		return result;
	}
	
	public long getValue() {
		return value;
	}

}
